---
title: "outcome_homicide_bor"
output: 
  html_document:
    toc: true
    toc_float:
      collapsed: true
      smooth_scroll: true
    depth: 3
    theme: cosmo
    highlight: pygments
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```

```{r, eval = TRUE}
library(readxl)
homicide <- read_excel("~/Documents/missouri_project/data/Homicide_Cov_Bor_1.xlsx")
```

```{r, eval = TRUE}
library(dplyr)
library(car)
```

```{r, eval = TRUE}
# select MO and the eight border states
data_homicide <- homicide %>% 
  filter(state=="Missouri" | state== "Iowa" | state=="Tennessee" | state=="Illinois" 
         | state=="Kentucky" | state=="Arkansas" | state=="Oklahoma" | state=="Kansas"
         | state=="Nebraska")
```

```{r, eval = TRUE}
# Remove the 2007 data
data_homicide <- data_homicide %>% filter(year!=2007)
```

```{r, eval = TRUE}
# Create an indicator variable about whether the subject is Missouri with year >= 2008
data_homicide$MO_2008ge <- 0
data_homicide$MO_2008ge[which(data_homicide$year>=2008 & data_homicide$state=="Missouri")] <- 1
```

```{r, eval = TRUE}
# Create another data frame with year only 2006, 2008-2013 data
data_homicide2 <- data_homicide %>% filter(year<=2013)
```

```{r, eval = TRUE}
# Turn state and year into factor variables
data_homicide$year <- as.factor(data_homicide$year)
data_homicide$state <- as.factor(data_homicide$state)

data_homicide2$year <- as.factor(data_homicide2$year)
data_homicide2$state <- as.factor(data_homicide2$state)
```

```{r, eval = TRUE}
### fit a linear model for year 2006, 2008-2019 data
fit1 <- lm(rate~state+year+MO_2008ge-1,data_homicide)
summary(fit1)
Anova(fit1)
```

```{r, eval = TRUE}
library(clubSandwich)
vcovCR(fit1, cluster = data_homicide$state, type = "CR2")
```

```{r, eval = TRUE}
### fit a linear model for year 2006, 2008-2013 data
fit2 <- lm(rate~state+year+MO_2008ge-1,data_homicide2)
summary(fit2)
Anova(fit2)
```

```
vcovCR(fit2, cluster = data_homicide2$state, type = "CR2")
```